﻿///<reference path="/Scripts/jquery-1.5.1.min.js">
var playListDatabase = function () {
    this.db = openDatabase("playlistdb", "1.0", "播放列表", 1024 * 1024, function () { });
    this.openStatus = null;
    
    if (!this.db) {
        alert("播放列表创建失败！");
    }
    else {        
        this.checkPlayWindowFlag();
    }
}
playListDatabase.prototype = {
    createPlayList: function () {
        this.db.transaction(function (tx) {
            tx.executeSql("create table if not exists playList (id INTEGER PRIMARY KEY AUTOINCREMENT,songID integer,name text,location text,artist text,guid text)",
                [],
                 null,
                function (tx, error)
                {
                    alert("create playList error:"+error.message);
                });
        });
    },

    createPlayingList:function(){
        this.db.transaction(function (tx) {
            tx.executeSql("create table if not exists playingList (id INTEGER PRIMARY KEY AUTOINCREMENT,songID integer,name text,location text,artist text,guid text)",
                [],
                 null,
                function (tx, error) {
                    alert("create playingList error:"+error.message);
                });
        });
    },

    addPlayList: function (id,name,location,artist) {
        this.db.transaction(function (tx) {
            tx.executeSql("insert into  playList (id,songID,name,location,artist,guid)values(null,?,?,?,?,?)",
                [id,name, location, artist, guidGenerator()],
                 null,
                function (tx, error) {
                    alert("add playList error:"+error.message);
                });
        });
    },
    addPlayingList: function (id,name, location, artist)
    {
        this.db.transaction(function (tx) {
            tx.executeSql("insert into  playingList (id,songID,name,location,artist,guid)values(null,?,?,?,?,?)",
                [id,name, location, artist,guidGenerator()],                  
                   null,
                function (tx, error) {
                    alert("add playingList error:"+error.message);
                });
        });
    },
    getPlayList: function (listID) {
        this.db.transaction(function (tx) {
            tx.executeSql("select * from playList",
                [],
                function(tx,result)
                {
                    for (var i = 0; i < result.rows.length; i++)
                    {
                        if ($("[data-val='" + $.trim(result.rows.item(i).guid) + "']").length == 0) {

                            $("#" + listID).append("<li><div class='div_PlayListItem' data-id=" + result.rows.item(i).songID + " data-val='" + $.trim(result.rows.item(i).guid) + "'  data-location='" + unescape(result.rows.item(i).location) + "'><div class='divPlay'></div><div style='float:left; width:300px;'>" + unescape(result.rows.item(i).name) + "--------" + unescape(result.rows.item(i).artist) + "</div></div></li>");
                        }
                    }
                },
                function (tx, error) {
                    alert("get playList error:" + error.message);
                });
        });
    },
    getPlayingList: function (listID) {
        this.db.transaction(function (tx) {
            tx.executeSql("select * from playingList",
                [],
                function (tx, result) {
                    for (var i = 0; i < result.rows.length; i++) {
                        if ($("[data-val='" + $.trim(result.rows.item(i).guid) + "']").length == 0) {

                            $("<li><div class='div_PlayListItem' data-id=" + result.rows.item(i).songID + " data-val='" + $.trim(result.rows.item(i).guid) + "'  data-location='" + unescape(result.rows.item(i).location) + "'><div class='divPlay'></div><div style='float:left; width:300px;'>" + unescape(result.rows.item(i).name) + "--------" + unescape(result.rows.item(i).artist) + "</div></div></li>").insertAfter("#" + listID);
                        }
                    }
                },
                function (tx, error) {
                    alert("get playingList error:"+error.message);
                });
        });
    },
    openPlayWindowFlag: function () {
        this.db.transaction(function (tx) {
            tx.executeSql("create table if not exists openStatus (flag text)",
                [],
                 function (tx,result) {
                     tx.executeSql("insert into openStatus(flag) values('flag')",[],null,null);
                 },
                function (tx, error) {
                    alert(error.message);
                });
        });
    },

    closePlayWindowFlag:function(){
        this.db.transaction(function (tx) {
            tx.executeSql("delete from openStatus",
                [],
                null,
                function (tx, error) {
                    alert(error.message);
                });
        });
    },
    checkPlayWindowFlag: function () {
        this.db.transaction(function (tx) {
            tx.executeSql("create table if not exists openStatus (flag text)",
                [],
                 function (tx, result) {
                     tx.executeSql("select count(*) as c from openStatus", [], function (tx, result) {
                         if (result.rows.item(0).c == 0) {
                             this.openStatus = false;
                         }
                         else {
                             this.openStatus = true;                             
                         }
                     }, null);
                 },
                function (tx, error) {
                    alert(error.message);
                });
        });
    }
}

function guidGenerator() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}














